from sklearn.feature_selection import SelectKBest
from scipy.stats import pearsonr
import numpy as np

from SBFI import func_sbf


def multivariate_pearsonr(X, Y):
    # 创建scores和pvalues数组，遍历数据集的每一列。
    scores, pvalues = [], []
    for column in range(X.shape[1]):
    # 只计算该列的皮尔逊相关系数和p值，并将其存储到相应数组中。
        cur_score, cur_p = pearsonr(X[:,column], Y)
        scores.append(abs(cur_score))
        pvalues.append(cur_p)
#函数最后返回包含皮尔逊相关系数和p值的元组。
    return (np.array(scores), np.array(pvalues))
def pearsonr_test(X,Y,S,df_rating):
    m_pearsonr = SelectKBest(score_func=multivariate_pearsonr, k=3)
    m_pearsonr.fit_transform(X, Y)
    # print(m_pearsonr.scores_)
    scores = []
    num_features = len(X.columns)
    for i in range(num_features):
        score = m_pearsonr.scores_[i]
        scores.append((score, i))
    b = sorted(scores, reverse=False)
    ranks = []
    for i in range(num_features):
        ranks.append(b[i][1])
    print(ranks)
    func_sbf(S, ranks, df_rating)
